package com.sailfish.springbootdemo.dao.db2;

import com.sailfish.springbootdemo.common.DB2TableName;
import com.sailfish.springbootdemo.pojo.db2.PcbCapacityDataHistory;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

import java.util.Date;
import java.util.List;

public interface PcbCapacityDataHistoryDao {
    @Results(id = "pcbCapacityDataHistoryResultMap", value = {
            @Result(column = "pcb_data_history_id", property = "pcbDataHistoryId", jdbcType = JdbcType.INTEGER),
            @Result(column = "pcb_data_version_name", property = "pcbDataVersionName", jdbcType = JdbcType.INTEGER),
            @Result(column = "pcb_factory_id", property = "pcbFactoryId", jdbcType = JdbcType.INTEGER),
            @Result(column = "pcb_capacity_param_id", property = "pcbCapacityParamId", jdbcType = JdbcType.INTEGER),
            @Result(column = "pcb_param_value_type", property = "pcbParamValueType", jdbcType = JdbcType.INTEGER),
            @Result(column = "pcb_param_key", property = "pcbParamKey", jdbcType = JdbcType.VARCHAR),
            @Result(column = "pcb_param_value", property = "pcbParamValue", jdbcType = JdbcType.VARCHAR),
            @Result(column = "create_time", property = "createTime", jdbcType = JdbcType.DATETIMEOFFSET),
            @Result(column = "update_time", property = "updateTime", jdbcType = JdbcType.DATETIMEOFFSET),
            @Result(column = "create_user", property = "createUser", jdbcType = JdbcType.VARCHAR),
            @Result(column = "update_user", property = "updateUser", jdbcType = JdbcType.VARCHAR),
            @Result(column = "status", property = "status", jdbcType = JdbcType.INTEGER),
            @Result(column = "sort", property = "sort", jdbcType = JdbcType.INTEGER),
    })

    /*
     * 查询单个
     */
    @Select("<script> " +
            "select *" +
            " from " + DB2TableName.PCB_CAPAICTY_DATA_HISTORY +
            "<where> " +
            "<if test='id != null'>" +
            " and pcb_data_history_id = #{id}" +
            "</if>" +
            "</where> " +
            " order by sort" +
            "</script>")
    List<PcbCapacityDataHistory> getById(@Param("id") Integer id);

    /*
     *查询指定行数据
     */
    @Select("<script> " +
            "select pcb_data_history_id, pcb_data_version_name, pcb_factory_id, pcb_capacity_param_id, pcb_param_value_type, pcb_param_key, pcb_param_value, create_time, create_user, update_time, update_user, status, sort " +
            "from " + DB2TableName.PCB_CAPAICTY_DATA_HISTORY +
            "<where>" +
            "<if test='pcbDataHistoryId != null'>" +
            "    and pcb_data_history_id = #{pcbDataHistoryId}" +
            "</if>" +
            "<if test='pcbDataVersionName != null '>" +
            "    and pcb_data_version_name = #{pcbDataVersionName}" +
            "</if>" +
            "<if test='pcbFactoryId != null '>" +
            "    and pcb_factory_id = #{pcbFactoryId}" +
            "</if>" +
            "<if test='pcbCapacityParamId != null '>" +
            "    and pcb_capacity_param_id = #{pcbCapacityParamId}" +
            "</if>" +
            "<if test='pcbParamValueType != null '>" +
            "    and pcb_param_value_type = #{pcbParamValueType}" +
            "</if>" +
            "<if test='pcbParamKey != null and pcbParamKey != \"\" '>" +
            "    and pcb_param_key = #{pcbParamKey}" +
            "</if>" +
            "<if test='pcbParamValue != null and pcbParamValue != \"\" '>" +
            "    and pcb_param_value = #{pcbParamValue}" +
            "</if>" +
            "<if test='sort != null'>" +
            "    and sort = #{sort}" +
            "</if>" +
            "<if test='status != null'>" +
            "    and status = #{status}" +
            "</if>" +
            "<if test='createTime != null'>" +
            "    and create_time = #{createTime}" +
            "</if>" +
            "<if test='updateTime != null'>" +
            "    and update_time = #{updateTime}" +
            "</if>" +
            "<if test='createUser != null and createUser != \"\"'>" +
            "    and create_user = #{createUser}" +
            "</if>" +
            "<if test='updateUser != null and updateUser != \"\"'>" +
            "    and update_user = #{updateUser}" +
            "</if>" +
            "</where>" +
            " order by sort" +
//            "limit #{pageable.offset}, #{pageable.pageSize}"+
            "</script>")

    @ResultMap(value = "pcbCapacityDataHistoryResultMap")
    List<PcbCapacityDataHistory> getAllByLimit(PcbCapacityDataHistory pcbCapacityDataHistory);

    /*
     *统计总行数
     */
    @Select("<script> " +
            "select count(1) " +
            "from " + DB2TableName.PCB_CAPAICTY_DATA_HISTORY +
            "</script>")
    Integer getAllCount();

    /*
     *新增所有列
     */
    @Insert("insert into " + DB2TableName.PCB_CAPAICTY_DATA_HISTORY + " (pcb_data_version_name, pcb_factory_id, pcb_capacity_param_id, pcb_param_value_type, pcb_param_key, pcb_param_value, create_time, create_user, update_time, update_user, status, sort) values " +
            "(#{pcbDataVersionName}, #{pcbFactoryId}, #{pcbCapacityParamId}, #{pcbParamValueType}, #{pcbParamKey}, #{pcbParamValue}, #{createTime}, #{createUser}, #{updateTime}, #{updateUser}, #{status}, #{sort})")
    @Options(useGeneratedKeys = true, keyProperty = "pcbDataHistoryId")
    Integer insert(PcbCapacityDataHistory param);

    /*
     *批量新增
     */
    @Insert("<script>" +
            "insert into " + DB2TableName.PCB_CAPAICTY_DATA_HISTORY +
            "(pcb_data_version_name, pcb_factory_id, pcb_capacity_param_id, pcb_param_value_type, pcb_param_key, pcb_param_value, create_time, create_user, update_time, update_user, status, sort) values " +
            "<foreach collection=\"entities\" item=\"entity\" separator=\",\">" +
            "(#{entity.pcbDataVersionName}, #{entity.pcbFactoryId}, #{entity.pcbCapacityParamId}, #{entity.pcbParamValueType}, #{entity.pcbParamKey}, #{entity.pcbParamValue}, #{entity.createTime}, #{entity.createUser}, #{entity.updateTime}, #{entity.updateUser}, #{entity.status}, #{entity.sort})" +
            "</foreach>" +
            "</script>")
    Integer insertBatch(@Param("entities") List<PcbCapacityDataHistory> entities);

    /*
     *通过主键删除
     */
    @Delete("delete from " + DB2TableName.PCB_CAPAICTY_DATA_HISTORY + " where pcb_data_history_id = #{id} ")
    Integer delete(@Param("id") Integer id);

    @Delete("delete from " + DB2TableName.PCB_CAPAICTY_DATA_HISTORY + " where  pcb_data_history_id in " +
            "<foreach collection=\"ids\" item = \"id\" open=\"(\" separator=\",\" close=\")\">  " +
            "#{id} " +
            "</foreach>"
    )
    Integer deleteBatch(@Param("ids") List<Integer> ids);

    @Delete("delete from " + DB2TableName.PCB_CAPAICTY_DATA_HISTORY + " where pcb_data_version_name = #{pcbDataVersionName}  ")
    Integer deleteByVersionName(String pcbDataVersionName);

    /*
     *通过主键修改数据
     */
    @Update("<script>" +
            "update " + DB2TableName.PCB_CAPAICTY_DATA_HISTORY +
            "<set>" +
            "<if test='pcbDataHistoryId != null '>" +
            "    pcb_data_history_id = #{pcbDataHistoryId}," +
            "</if>" +
            "<if test='pcbDataVersionName != null and pcbDataVersionName != \"\" '>" +
            "    pcb_data_version_name = #{pcbDataVersionName}," +
            "</if>" +
            "<if test='pcbFactoryId != null '>" +
            "    pcb_factory_id = #{pcbFactoryId}," +
            "</if>" +
            "<if test='pcbCapacityParamId != null '>" +
            "    pcb_capacity_param_id = #{pcbCapacityParamId}," +
            "</if>" +
            "<if test='pcbParamValueType != null '>" +
            "    pcb_param_value_type = #{pcbParamValueType}," +
            "</if>" +
            "<if test='pcbParamKey != null and pcbParamKey != \"\" '>" +
            "    pcb_param_key = #{pcbParamKey}," +
            "</if>" +
            "<if test='pcbParamValue != null and pcbParamValue != \"\" '>" +
            "    pcb_param_value = #{pcbParamValue}," +
            "</if>" +
            "<if test='sort != null'>" +
            "    sort = #{sort}," +
            "</if>" +
            "<if test='status != null'>" +
            "    status = #{status}," +
            "</if>" +
            "<if test='createTime != null'>" +
            "    create_time = #{createTime}," +
            "</if>" +
            "<if test='updateTime != null'>" +
            "    update_time = #{updateTime}," +
            "</if>" +
            "<if test='createUser != null and createUser != \"\"'>" +
            "    create_user = #{createUser}," +
            "</if>" +
            "<if test='updateUser != null and updateUser != \"\"'>" +
            "    update_user = #{updateUser}," +
            "</if>" +
            "</set>" +
            "where pcb_data_history_id = #{pcbDataHistoryId}" +
            "</script>")
    Integer update(PcbCapacityDataHistory param);

}
